package com.huaxindata.im.view;

import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.huaxindata.im.R;
import com.huaxindata.im.model.entity.ChatMessage;
import com.huaxindata.im.model.entity.ChatMessageBody;


public class ChatPopWindow extends PopupWindow implements View.OnClickListener {

    private TextView copy;
    //private TextView send;
    private TextView delete;
    private ChatMessage tag;
    private int w, h;
    View content;
    View  div2;


    public ChatPopWindow(Context context) {
        super(context);
        content = LayoutInflater.from(context).inflate(R.layout.common_chat_dialog, null);
        setContentView(content);
        setOutsideTouchable(true);
//        setFocusable(true);
        setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
        setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        setBackgroundDrawable(new BitmapDrawable());
        copy = (TextView) content.findViewById(R.id.tv_copy);
      //  send = (TextView) content.findViewById(R.id.tv_send);
        delete = (TextView) content.findViewById(R.id.tv_del);
      //  div1 = content.findViewById(R.id.div1);
        div2 = content.findViewById(R.id.div2);
        copy.setOnClickListener(this);
      //  send.setOnClickListener(this);
        delete.setOnClickListener(this);
        content.measure(0, 0);
        w = content.getMeasuredWidth();
        h = content.getMeasuredHeight();
    }

    public void show(final View attach, final int x, final int y, final ChatMessage tag) {
        this.tag = tag;
        int realx = x - w / 2;
        if (realx < 0) {
            realx = 0;
        }
        setupViewWithTag(tag);
//        Log.d("show :" + w + "-" + h);
        showAtLocation(attach, Gravity.NO_GRAVITY, realx, y - h);

    }

    private void setupViewWithTag(ChatMessage tag) {
//        if (tag.type == ChatMessageBody.Type.VOICE) {
//           // send.setVisibility(View.GONE);
//            div2.setVisibility(View.GONE);
//        } else {
//          //  send.setVisibility(View.VISIBLE);
//            div2.setVisibility(View.VISIBLE);
//        }
        if (tag.type == ChatMessageBody.Type.TEXT) {
            copy.setVisibility(View.VISIBLE);
            //div1.setVisibility(View.VISIBLE);
            div2.setVisibility(View.VISIBLE);
        } else {
            copy.setVisibility(View.GONE);
          //  div1.setVisibility(View.GONE);
            div2.setVisibility(View.GONE);
        }
    }


    @Override
    public void onClick(View v) {
        if (callBack != null) {
            callBack.callback(tag, v.getId());
        }
        tag = null;
        dismiss();
    }

    ActionCallBack callBack;

    public void setCallBack(ActionCallBack callBack) {
        this.callBack = callBack;
    }

    public interface ActionCallBack {
        /**
         * 操作结束后的回调
         *
         * @param message 当前处理的消息对象
         * @param resID   操作类型
         */
        void callback(ChatMessage message, int resID);
    }
}
